package com.xfan.core.util;


import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class JsonUtil {
	public static final JsonConfig cfg=new JsonConfig();
	static{
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessorImpl());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateValueProcessorImpl());
		cfg.registerJsonValueProcessor(java.sql.Timestamp.class, new JsonDateValueProcessorImpl());
	}
	public static JsonConfig getJsonConfig(String dataformat){
		JsonConfig cfg=new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessorImpl(dataformat));
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateValueProcessorImpl(dataformat));
		cfg.registerJsonValueProcessor(java.sql.Timestamp.class, new JsonDateValueProcessorImpl(dataformat));
		return cfg;
	}
	
	public static String getJson(Object target){
		return getJson(cfg);
	}
	
	public static String getJson(Object target,JsonConfig cfg){
		if (target instanceof List) {
			JSONArray arr = JSONArray.fromObject(target,cfg);
			return arr.toString();
		}
		JSONObject obj = JSONObject.fromObject(target,cfg);
		return obj.toString();
	}
}
